//
//  ByCellCommitOrderOrderInfo.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellCommitOrderOrderInfo.h"

static CGFloat kSingleHeight = 44;

// 记录tag
static CGFloat kExpressContentTag = 100;
static CGFloat kNoteTag = 200;
static CGFloat kSumTag = 300;


@implementation ByCellCommitOrderOrderInfo
{
    UIView *_expressView;       // 快递
    UIView *_noteView;          // 留言
    UIView *_sumView;           // 合计
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
// 快递
- (void)setupExpressView {
    
    _expressView = [[UIView alloc] init];
    [self.contentView addSubview:_expressView];
    
    // 标题
    NSString *text = @"配送方式";
    CGFloat x, y, width, height;
    x = BY_TABLE_LEFT_MARGIN;
    y = 0;
    width = [text getSizeOfString:14].width + 10;
    height = kSingleHeight;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.font = FONT(14);
    titleLabel.text = text;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [_expressView addSubview:titleLabel];
    
    // 右侧箭头
    x = 0;
    y = 0;
    width = 12;
    height = width;
    UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    arrowView.image = [UIImage imageNamed:@"Order_arrow"];
    [_expressView addSubview:arrowView];
    arrowView.y = kSingleHeight / 2;
    arrowView.right = SCREEN_WIDTH - 10;
    
    x = titleLabel.right;
    y = 0;
    width = arrowView.left - 10 - x;
    height = kSingleHeight;
    UILabel *expressLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    expressLabel.adjustsFontSizeToFitWidth = YES;
    expressLabel.font = FONT(14);
    expressLabel.textColor = [UIColor blackColor];
    expressLabel.textAlignment = NSTextAlignmentRight;
    [_expressView addSubview:expressLabel];
    expressLabel.tag = kExpressContentTag;
    
    // 分割线
    x = BY_TABLE_LEFT_MARGIN;
    width = SCREEN_WIDTH - x * 2;
    height = 0.5;
    y = kSingleHeight - height;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = BY_LINE_COLOR;
    [_expressView addSubview:line];
}
// 留言
- (void)setupNoteView {
    
    _noteView = [[UIView alloc] init];
    [self.contentView addSubview:_noteView];
    
    // 标题
    NSString *text = @"买家留言：";
    CGFloat x, y, width, height;
    x = BY_TABLE_LEFT_MARGIN;
    y = 0;
    width = [text getSizeOfString:14].width + 10;
    height = kSingleHeight;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.font = FONT(14);
    titleLabel.text = text;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [_noteView addSubview:titleLabel];
    
    // 留言框
    x = titleLabel.right;
    y = 0;
    width = SCREEN_WIDTH - 10 - x;
    height = kSingleHeight;
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
    tf.placeholder = @"选填：对本次交易的说明";
    tf.textColor = [UIColor blackColor];
    tf.font = FONT(13);
    [_noteView addSubview:tf];
    tf.tag = kNoteTag;
    tf.delegate = self;
    
    // 分割线
    x = BY_TABLE_LEFT_MARGIN;
    width = SCREEN_WIDTH - x * 2;
    height = 0.5;
    y = kSingleHeight - height;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = BY_LINE_COLOR;
    [_noteView addSubview:line];
}

// 合计
- (void)setupSumView {
    
    _sumView = [[UIView alloc] init];
    _sumView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_sumView];
    
    
    CGFloat x, y, width, height;
    x = 0;
    y = 0;
    width = SCREEN_WIDTH - 10;
    height = kSingleHeight;
    UILabel *sumLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    sumLabel.font = FONT(13);
    sumLabel.textColor = [UIColor blackColor];
    sumLabel.textAlignment = NSTextAlignmentRight;
    [_sumView addSubview:sumLabel];
    sumLabel.tag = kSumTag;
    
    // 分割线
    x = 0;
    width = SCREEN_WIDTH;
    height = 0.5;
    y = kSingleHeight - height;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    [_sumView addSubview:line];
}

- (void)setupUI {
    
    [self setupExpressView];
    [self setupNoteView];
    [self setupSumView];
}

- (void)setStoreModel:(ByModelShopCar *)storeModel {
    
    _storeModel = storeModel;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat x, y, width, height;
    // 快递
    x                       = 0;
    y                       = 0;
    width                   = SCREEN_WIDTH;
    height                  = kSingleHeight;
    _expressView.frame      = CGRectMake(x, y, width, height);
    
    UILabel *expressLabel   = [_expressView viewWithTag:kExpressContentTag];
    expressLabel.text       = @"快递 免邮";
    
    // 留言
    x                       = _expressView.left;
    y                       = _expressView.bottom;
    width                   = _expressView.width;
    height                  = _expressView.height;
    _noteView.frame         = CGRectMake(x, y, width, height);
    
    // 合计
    x                       = _noteView.left;
    y                       = _noteView.bottom;
    width                   = _noteView.width;
    height                  = _noteView.height;
    _sumView.frame          = CGRectMake(x, y, width, height);
    
    UILabel *sumLabel       = [_sumView viewWithTag:kSumTag];
    NSInteger count         = _storeModel.item_list.count;
    __block CGFloat totalAmount = 0;
    [_storeModel.item_list enumerateObjectsUsingBlock:^(ByModelShopCarListItem *itemModel, NSUInteger index, BOOL * _Nonnull stop) {
        totalAmount += (itemModel.price.floatValue * itemModel.count.integerValue);
    }];
    NSString *sumText       = [NSString stringWithFormat:@"共%li件商品 小计：￥%.2lf", count, totalAmount];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:sumText];
    NSRange maohaoRange = [sumText rangeOfString:@"："];
    NSRange priceRange = NSMakeRange(maohaoRange.location + 1, sumText.length - maohaoRange.location - 1);
    NSRange integeRange = NSMakeRange(priceRange.location + 1, priceRange.length - 4);
    [attr addAttribute:NSForegroundColorAttributeName value:KShopCarThemeColor range:priceRange];
    [attr addAttribute:NSFontAttributeName value:FONT(20) range:integeRange];

    sumLabel.attributedText = attr;
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (self.notesFetchBlock) {
        self.notesFetchBlock(textField.text);
    }
}

@end
